namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorPositiveVolumeIndex : BaseIndicator
    {
        private LineShape tlsMPvi;
        private LineShape tlsPvi;

        public IndicatorPositiveVolumeIndex()
        {
            base.IndType = IndicatorType.PVI;
            base.IndName = "PVI";
            base.IndDesc = "正成交量指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 72.0));
            base.SourceDic["Volume"] = "";
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "PVI" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsPvi) && (obj != this.tlsMPvi))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            if (r == 0)
            {
                return new double[] { 1.0, (1.0 / base.IndPeriod[0].ParamValue) };
            }
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Volume"]]);
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Volume"]]);
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][this.tlsPvi.Name]);
            double curValue = 0.0;
            if (num > num2)
            {
                double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
                double num6 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Close"]]);
                curValue = num3 + (((num5 - num6) / num6) * num3);
            }
            else
            {
                curValue = num3;
            }
            double num7 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.tlsMPvi.Name, this.tlsPvi.Name, curValue, base.Crab.DataSource);
            return new double[] { curValue, num7 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsPvi, this.tlsMPvi };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.tlsMPvi);
            base.Crab.DeleteShape(this.tlsPvi);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.tlsPvi.Name, numArray[0], r);
                        base.Crab.SetValue(this.tlsMPvi.Name, numArray[1], r);
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["Volume"] = source[0];
            base.SourceDic["Close"] = source[1];
            base.IndPeriod[0].ParamValue = periods[0];
            this.TlsPvi = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMPvi = base.Crab.AddLine(fields[1], panelID);
        }

        public LineShape TlsMPvi
        {
            get
            {
                return this.tlsMPvi;
            }
            set
            {
                this.tlsMPvi = value;
            }
        }

        public LineShape TlsPvi
        {
            get
            {
                return this.tlsPvi;
            }
            set
            {
                this.tlsPvi = value;
            }
        }
    }
}

